import Vue from 'vue'
import Vuex from 'vuex'
import { getItem, setItem } from '../utils/storages.js'

Vue.use(Vuex)
const TOKEN_KEY = 'TOKEN_USER'

export default new Vuex.Store({
  state: {
    // 页面加载时，立即读取数据
    user: getItem(TOKEN_KEY)
  },
  mutations: {
    setUser (state, user) {
      state.user = user
      // 避免刷新后数据消失(将数据存储到本地内存中)
      setItem(TOKEN_KEY, state.user)
    }
  },
  actions: {},
  modules: {}
})
